<template>
    <div class="main" @click="clickOut" ref="mainBox">
        <div class="share-box relative">
            <div class="head">
                <h1 class="title">文件分享</h1>
                <a class="close" @click="closeFun">x</a>
            </div>
            <div class="content" v-show="!showLink">
                <div class="share-type">
                    <h1 class="fl">分享方式</h1>
                    <div class="list fl">
                        <ul>
                            <li>
                                <label class="fl pointer">
                                    <input type="radio" value="1" v-model="status">
                                    加密分享
                                </label>
                                <p class="fl">仅拥有密码者可查看下载</p>
                            </li>
                            <!--<li>-->
                                <!--<label class="fl pointer">-->
                                    <!--<input type="radio" value="2" v-model="status">-->
                                    <!--公开分享-->
                                <!--</label>-->
                                <!--<p class="fl">拥有分享链接的用户可直接打开查看或下载</p>-->
                            <!--</li>-->
                            <li>
                                <label class="fl pointer">
                                    <input type="radio" value="3" v-model="status">
                                    手机号分享
                                </label>
                                <p class="fl">需要指定手机号码的用户输入正确的验证码才可查看或下载</p>
                                <input class="phone" placeholder="请输入接收文件的手机号码" v-show="status === '3'" v-model="phone">
                            </li>
                        </ul>
                    </div>
                    <div style="clear: both"></div>
                </div>
                <div class="time">
                    <h1 class="fl">分享有效期</h1>
                    <div class="time-select-box fl">
                        <div class="choose-time text-center" @click.stop="showTimeList"
                             v-text="checkExpireType()"></div>
                        <ul class="select-time-list" v-show="timeList" ref="timeList">
                            <li @click="changeExpireType(1)">一天有效</li>
                            <li @click="changeExpireType(2)">七天有效</li>
                            <li @click="changeExpireType(3)">永久有效</li>
                        </ul>
                    </div>
                </div>
                <div class="button-box">
                    <button class="cencel" @click="closeFun">取消</button>
                    <button class="create-link" @click.stop="createLink">创建链接</button>
                </div>
            </div>
            <div class="link-content relative" v-show="showLink">
                <div class="label">
                    <img class="fl" src="@/assets/icon/over_icon.png">
                    <p class="fl">创建链接成功</p>
                </div>
                <div class="link">
                    <h1 class="label fl">链接：</h1>
                    <div class="link-box" v-text="shareLink"></div>
                </div>

                <div class="link" v-if="password !==''">
                    <h1 class="label fl">密码：</h1>
                    <div class="link-box" v-text="password" style="width: 100px"></div>
                </div>
                <input ref="copyInput" style="opacity: 0">

                <div class="button-box">
                    <button class="create-link" @click.stop="copylink">复制链接</button>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
  export default {
    name: "Share",
    props: {
      fileList: {
        type: Array,
        default: function () {
          return []
        }
      },
      chooseList: {
        type: Array,
        default: function () {
          return []
        }
      },
      closeFun: {
        type: Function,
        default: function () {
          console.log('---------')
        }
      }
    },
    data() {
      return {
        shareLink: '',
        showLink: false,
        phone: '',
        status: 1,
        timeList: false,
        expireType: 1,
        password: '',
        test:''
      }
    }, methods: {
      showTimeList: function () {
        this.timeList = true
      },
      clickOut: function (event) {
        if(!this.$refs.timeList) return
        if (!this.$refs.timeList.contains(event.target)) {
          this.timeList = false
        }
      },
      changeExpireType: function (type) {
        this.expireType = type
        this.timeList = false
      },

      checkExpireType: function () {
        if (this.expireType === 1) {
          return '一天有效'
        } else if (this.expireType === 2) {
          return '七天有效'
        } else if (this.expireType === 3) {
          return '永久有效'
        }
      },
      getFileName: function () {
        const that = this
        const firstFileData = this.fileList.find(function (val) {
          return that.chooseList[0] === val.fileId
        })
        if (this.chooseList.length === 1) {
          return firstFileData.name
        } else {
          return firstFileData.name + '等'
        }
      },
      getExpireTime: function () {
        let now = new Date().getTime()
        if (this.expireType === 1) {
          return now + 24 * 3600 * 1000
        } else if (this.expireType === 2) {
          return now + 7 * 24 * 3600 * 1000
        } else if (this.expireType === 3) {
          return 4000000000000
        }
      },
      checkFileType: function () {
        const that = this;
        const firstFileData = this.fileList.find(function (val) {
          return that.chooseList[0] === val.fileId
        })
        if (this.chooseList.length > 1) {
          return 2
        } else if (this.chooseList.length == 1 && firstFileData.fileType === 1) {
          return 1
        } else {
          return 0
        }
      },
      copylink: function () {
        let input = this.$refs.copyInput
        let text = '【担当云盘】为您带来高效的文件存储于传输体验！ 分享链接：' + this.shareLink
        if (this.password !== '') {
          text = text + '  提取码: ' + this.password // 修改文本框的内容
        }
        input.value = text
        input.select(); // 选中文本
        console.log(document.execCommand("copy"))
        document.execCommand("copy");
        this.$message({
          type: 'success',
          message: '复制成功'
        })

        this.$http.get('/pan/shareInitInfo/' + this.test).then(function (data) {
          console.log(data)
        })
      },
      createLink: function () {
        const that = this
        if (this.status == 3 && (!this.phone || this.phone === '')) {
          return this.$message({
            type: 'error',
            message: '请填写手机号'
          })
        }
        const fileName = this.getFileName()
        const expireTime = this.getExpireTime()
        const fileType = this.checkFileType()
        const companyInfo = this.$store.state.company.companyInfo
        let postData = {
          displayName: fileName,
          expireTime: expireTime,
          fileType: fileType,
          ids: this.chooseList,
          organizationId: companyInfo.companyId,
          type: this.status
        }
        console.log(postData)
        if (this.phone && this.phone !== '') {
          postData.phone = this.phone
        }
        this.$http.post('/pan/shareFiles', postData).then(function (data) {
          if (data.data.code === 1) {
            that.showLink = true
            that.password = data.data.data.password
            that.shareLink = window.location.origin + that.$config.shareLink + data.data.data.url
            that.test = data.data.data.url
          }
          console.log(data)
        })
      }
    }
  }
</script>

<style scoped>
    .main {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.49);
    }

    .share-box {
        width: 660px;
        position: absolute;
        top: calc(50vh - 180px);
        background: white;
        left: 0;
        right: 0;
        margin: auto;
        border-radius: 10px;
    }

    .share-box .head {
        height: 40px;
        border-bottom: 1px solid #ccc;
    }

    .share-box .head .title {
        line-height: 40px;
        font-size: 14px;
        color: #323232;
        padding-left: 20px;
        float: left;
    }

    .share-box .head .close {
        float: right;
        line-height: 36px;
        font-size: 20px;
        margin-right: 20px;
        cursor: pointer;
    }

    .share-box .content {
        padding: 20px 20px;
        height: 190px;
    }

    .share-box .content h1 {
        font-size: 14px;
    }

    .share-box .content .share-type .list {
        margin-left: 30px;
        width: 500px;
    }

    .share-box .content .share-type ul li {
        min-height: 35px;
    }

    .share-box .content .share-type ul li label {
        width: 110px;
        font-size: 14px;
    }

    .share-box .content .share-type ul li p {
        color: #999;
        font-size: 14px;
    }

    .share-box .content .share-type ul li .phone {
        font-size: 14px;
        height: 35px;
        border-radius: 5px;
        border: 1px solid #DCDFE6;
        width: 360px;
        float: left;
        margin-left: 110px;
        margin-top: 10px;
        margin-bottom: 10px;
        box-sizing: border-box;
        color: #606266;
        padding-left: 10px;
    }

    .share-box .content .share-type ul li .phone::placeholder {
        font-size: 14px;
        color: #DCDFE6;
    }


    .share-box .content .time-select-box {
        margin-left: 15px;
    }

    .share-box .content .time-select-box .choose-time {
        cursor: pointer;
        position: relative;
        width: 143px;
        height: 32px;
        border: 1px solid rgba(151, 151, 151, 0.66);
        line-height: 30px;
        font-size: 14px;
        color: #333333;
        letter-spacing: -0.34px;
        border-radius: 2px;
        padding-right: 10px;
        box-sizing: border-box;
    }

    .share-box .content .time-select-box .select-time-list {
        background: #FFFFFF;
        box-shadow: 0 1px 6px 0 rgba(157, 163, 165, 0.50);
        width: 143px;
        padding: 12px 0;
        box-sizing: border-box;
    }

    .share-box .content .time-select-box .select-time-list li {
        font-size: 14px;
        color: #666666;
        letter-spacing: 1px;
        line-height: 30px;
        text-align: center;
        cursor: pointer;
    }

    .share-box .content .time-select-box .select-time-list li:hover {
        background: rgba(220, 222, 227, 0.61);
    }

    .share-box .content .time-select-box .choose-time:before {
        position: absolute;
        content: '';
        right: 15px;
        top: 13px;
        border-width: 5px 4px 0;
        border-style: solid;
        border-color: #6D6E8C transparent transparent;
    }


    .share-box .content .time h1 {
        line-height: 33px;
    }

    .button-box {
        position: absolute;
        bottom: 20px;
        right: 20px;
    }

    .button-box button {
        width: 98px;
        height: 40px;
        font-size: 14px;
        border-radius: 3px;
        margin-left: 20px;
    }

    .button-box .create-link {
        color: #FFF;
        background-color: #409EFF;
        border-color: #409EFF;
    }

    .button-box .cencel {
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        background: #FFF;
        border: 1px solid #DCDFE6;
        color: #606266;
    }

    .link-content {
        padding: 20px 20px;
        height: 200px;
    }

    .link-content .label {
        line-height: 30px;
        font-size: 14px;
        height: 50px;
    }

    .link-content img {
        margin-right: 10px;
    }

    .link-content .link {
        height: 50px;

    }

    .link-content .link h1 {
        line-height: 44px;
    }

    .link-content .link .link-box {
        border-radius: 4px;
        border: 1px solid #d7d7d7;
        float: left;
        height: 40px;
        width: 500px;
        margin-left: 20px;
        line-height: 40px;
        padding-left: 15px;

    }

</style>
